#include <iostream>
#include <cerrno>
#include <string>
#include <time.h>
#include "lib/Socket.hpp"
#include "lib/Protocol.hpp"

using namespace ns_protocol;

static void Usage(const char *str)
{
    std::cout << "\n Usage:" << str << " ip port\n"
              << std::endl;
}

static const char g_op[] = {
    '+',
    '-',
    '*',
    '/',
    '%'};

// 客户端写的有点问题，需要改成多线程模式最好

// ./ CalClient ip port
int main(int argc, char *argv[])
{
    // if (argc != 3)
    // {
    //     Usage(argv[0]);
    //     exit(1);
    // }
    srand((unsigned long)time(nullptr));
    Sock sock;
    int sockfd = sock.Socket();
    // std::string server_ip = argv[1];
    // uint16_t server_port = atoi(argv[2]);
    std::string server_ip = "127.0.0.1";
    uint16_t server_port = 8080;
    if (sock.Connect(sockfd, server_ip, server_port) == false)
    {
        std::cout << "connect error" << std::endl;
        exit(1);
    }
    std::cout << "任务格式：\na + b" << std::endl;
    Sock::NonBlock(sockfd);
    // 客户端执行
    while (true)
    {
        // 发送多个
        for (int i = 0; i < 50; i++)
        {
            // std::cout << "请输入任务：" << std::endl;
            // // 输入请求
            // Request req;
            // std::cin >>  req._x >> req._op >> req._y;
            // // 序列化

            Request req;
            req._x = rand() % 100;
            req._op = g_op[rand() % 5];
            req._y = rand() % 100;

            std::string str = req.Serialize();
            // std::string str;
            // std::getline(std::cin, str);
            if (str.empty())
                continue;
            // 添加报头
            std::string send_message = Encode(str);

            // 发送
            ssize_t sz = send(sockfd, send_message.c_str(), send_message.size(), 0);
            // std::cout << "send count:" << i << " " << send_message.size() << std::endl;
            if (sz <= 0)
            {
                std::cout << "send error:" << errno << " " << strerror(errno) << std::endl;
                exit(3);
            }
        }
        // 接收
        char buffer[10240];
        while (true)
        {
            ssize_t sz = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
            if (sz > 0)
            {
                buffer[sz] = 0;
            }
            else if (sz == 0)
            {
                std::cout << "服务器关闭了" << std::endl;
                exit(1);
            }
            else
            {
                if (errno == EAGAIN || errno == EWOULDBLOCK)
                {
                    Messagelog(ERROR, "3333");

                    break; // 没有空间了
                }
                else if (errno == EINTR)
                    continue;
                else
                {
                    exit(3);
                }
            }
        }

        std::string recv_str = buffer;

        while (true)
        {
            std::string message;
            // 剪去报头
            message = Decode(recv_str);
            if (message.empty())
                break;
            // std::cout << "!!!!!!!!!!" << message << std::endl;

            // 反序列化
            Response res;
            res.Deserialize(message);
            std::cout << res._x << " " << res._op << " " << res._y;

            // 结果展示
            if (res._status == 0)
            {
                std::cout << " = " << res._result << std::endl;
            }
            else
            {
                std::cout << res.error() << std::endl;
            }
        }
    }

    return 0;
}